package mx.ags.miforo.dao;


import java.util.Iterator;
import java.util.List;
import mx.ags.miforo.bean.Usuario;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;





public class UsuarioDaoImpl implements Dao {
	
	
    public Integer Add(){
	  
	  Session session = DaoFactory.getFactory().openSession();
      Transaction tx = null;
      Integer employeeID = null;
      try{
         tx = session.beginTransaction();
         Usuario usr = new Usuario();
         employeeID = (Integer) session.save(usr); 
         tx.commit();
      }catch (HibernateException e) {
         if (tx!=null) tx.rollback();
         e.printStackTrace(); 
      }finally {
         session.close(); 
      }
      return employeeID;
	  
  }
	
	public void List(){
	     Session session = DaoFactory.getFactory().openSession();
	      Transaction tx = null;
	      try{
	         tx = session.beginTransaction();
	         List employees = session.createQuery("FROM Employee").list(); 
	         for (Iterator iterator = 
	                           employees.iterator(); iterator.hasNext();){
	            Usuario usr = (Usuario) iterator.next(); 
	            System.out.print("First Name: " + usr.getNombre()); 
	            System.out.print("  Last Name: " + usr.getAppaterno()); 
	            System.out.println(" Correo: " + usr.getMail()); 
	         }
	         tx.commit();
	      }catch (HibernateException e) {
	         if (tx!=null) tx.rollback();
	         e.printStackTrace(); 
	      }finally {
	         session.close(); 
	      }
		
	}
	
	public void Delete(Integer UsuarioId){
	    Session session = DaoFactory.getFactory().openSession();
	      Transaction tx = null;
	      try{
	         tx = session.beginTransaction();
	         Usuario employee = 
	                   (Usuario)session.get(Usuario.class,UsuarioId ); 
	         session.delete(employee); 
	         tx.commit();
	      }catch (HibernateException e) {
	         if (tx!=null) tx.rollback();
	         e.printStackTrace(); 
	      }finally {
	         session.close(); 
	      }
		
	}
	
	public void Update(Integer UsuarioId){
		  Session session = DaoFactory.getFactory().openSession();
	      Transaction tx = null;
	      try{
	         tx = session.beginTransaction();
	         Usuario usr = 
	                    (Usuario)session.get(Usuario.class, UsuarioId); 
	        usr.setMail("anticarbono@gmail.com");
			 session.update(usr); 
	         tx.commit();
	      }catch (HibernateException e) {
	         if (tx!=null) tx.rollback();
	         e.printStackTrace(); 
	      }finally {
	         session.close(); 
	      }
		
	}



}
